﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Repository.Entities;
using System.Data.Common;
using System.Data;

namespace Repository.SecurityRepository
{
    public class UserRepository
    {
        private DAL.DBProvider _dbProvider = new DAL.DBProvider();

        /// <summary>
        /// 创建用户
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public User CreateUser(Entities.User user)
        {
            Utils.AssertUtils.IsNotNull(user, "user is null");
            Utils.AssertUtils.IsNotNullAndEmpty(user.UserId, "user.UserId is empty");
            user.CreateDate = Utils.DateTimeHelper.Now;
            user.LastLoginDate = user.CreateDate;

            using (DbConnection conn = _dbProvider.CreateConnection())
            {
                conn.Open();

                string sql = string.Format(@"
                    insert into user(UserId,Password,Email,IsLocked,CreateDate,LastLoginDate)
                                values({0},{1},{2},{3},{4},{5})
                ",_dbProvider.BuildParameterName("UserId"),
                  _dbProvider.BuildParameterName("Password"),
                  _dbProvider.BuildParameterName("Email"),
                  _dbProvider.BuildParameterName("IsLocked"),
                  _dbProvider.BuildParameterName("CreateDate"),
                  _dbProvider.BuildParameterName("LastLoginDate"));

                using (DbCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    cmd.CommandType = CommandType.Text;

                    _dbProvider.AddParameter(cmd, "UserId", DbType.String, user.UserId);
                    _dbProvider.AddParameter(cmd, "Password", DbType.String, user.Password);
                    _dbProvider.AddParameter(cmd, "Email", DbType.String, user.Email);
                    _dbProvider.AddParameter(cmd, "IsLocked", DbType.Boolean, user.IsLocked);
                    _dbProvider.AddParameter(cmd, "CreateDate", DbType.DateTime, user.CreateDate);
                    _dbProvider.AddParameter(cmd, "LastLoginDate", DbType.DateTime, user.LastLoginDate);

                    cmd.ExecuteNonQuery();
                }

                conn.Close();
            }

            return user;
        }

        /// <summary>
        /// 获取密码
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public string GetPassword(string userId)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="oldPassword"></param>
        /// <param name="newPassword"></param>
        /// <returns></returns>
        public bool ChangePassword(string userId, string oldPassword, string newPassword)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 更新用户（不包括密码信息）
        /// </summary>
        /// <param name="user"></param>
        public void UpdateUser(User user)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 检查用户与密码是否正确
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool ValidateUser(string userId, string password)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 锁住用户
        /// </summary>
        /// <param name="userId"></param>
        public void LockUser(string userId)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 解锁用户
        /// </summary>
        /// <param name="userId"></param>
        public void UnLockUser(string userId)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="userId"></param>
        public void DeleteUser(string userId)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 通过用户Id查找用户
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public User FindUserByUserId(string userId)
        {
            throw new NotImplementedException();
        }
    }
}
